package cn.leyaner.hrm.soket;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;

/*
   服务器
 */
public class Server  extends JFrame implements ActionListener {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JTextArea jta = null;
    JTextField jtf = null;
    JButton jb = null;
    JPanel jpl = null;
    JScrollPane jsp = null;
    //把信息发给客户端的对象
    PrintWriter pw = null;

    public static void main(String[] args) {
        Server server = new Server();
    }

    public Server() {
        jta = new JTextArea();
        jtf = new JTextField(20);
        jb = new JButton("发送");
        jb.addActionListener(this);
        jpl = new JPanel();
        jsp = new JScrollPane(jta);
        jpl.add(jtf);
//jpl.add(jta);
        jpl.add(jb);
        this.add(jsp, "Center");
        this.add(jpl, "South");
        this.setTitle("服务端");
        this.setSize(400, 300);
        this.setVisible(true);
           //服务器监听
        try {
            ServerSocket ss = new ServerSocket(9988);
               //接收等待客户端连接
            Socket s = ss.accept();
                //读取客户端发来的信息
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            BufferedReader brd = new BufferedReader(isr);
            pw = new PrintWriter(s.getOutputStream(), true);
            while (true) {
                   //读取客户端信息
                String info = brd.readLine();
               //把客户端信息写到信息栏
                jta.append("客户端：" + info + "\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        @Override
        public void actionPerformed(ActionEvent  e) {
           //如果用户按下发送信息按钮
            if(e.getSource()==jb){
            //把服务器在框里写内容发送给客户端
                String info = jtf.getText();
                jta.append("服务端："+info+"\r\n");
                pw.println(info);//发送
                jtf.setText("");//清空输入框
                Object obj = e.getSource();
            }
        }

    }
